<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>includeDefaultValues</title>
</head>
<body>
  <div>
    <button onclick="toObject()">toObject</button>
  </div>
  <canvas id="canvasBox" width="600" height="600" style="border: 1px solid #ccc;"></canvas>

<script src="../../script/fabric.js"></script>
<script>
  // 使用 元素id 创建画布，此时可以在画布上框选
  const canvas = new fabric.Canvas('canvasBox', {
    // includeDefaultValues: true // 默认 true
    includeDefaultValues: false // 指示toObject/toDatalessObject是否应该包含默认值，如果设置为false，则优先于对象值
  })

  fabric.Image.fromURL('../../images/bg.jpg', img => {
    canvas.setBackgroundImage(img)
    canvas.renderAll()
  })

  // 圆形
  circle = new fabric.Circle({
    id: 1,
    name: 'circle',
    top: 60,
    left: 60,
    radius: 30, // 圆的半径 30
    fill: 'yellowgreen'
  })

  // 矩形
  rect = new fabric.Rect({
    id: 2,
    name: 'rect',
    top: 30, // 距离容器顶部 60px
    left: 100, // 距离容器左侧 200px
    fill: 'orange', // 填充a 橙色
    width: 60, // 宽度 60px
    height: 60 // 高度 60px
  })

  // 将矩形添加到画布中
  canvas.add(circle, rect)

  function toObject() {
    const res = canvas.toObject()
    console.log(res)
  }
</script>
</body>
</html>